// This is the main DLL file.

#include "stdafx.h"
#include "Tilt.h"

using namespace RBarreiros;

AudioEffect* createEffectInstance(audioMasterCallback audioMaster)
{
	return new Tilt(audioMaster);
}

Tilt::Tilt(audioMasterCallback audioMaster) : 
	AudioEffectX(audioMaster, 1, kParamCount)
{
	setUniqueID(CCONST('t', 'i', 'l', 't'));
	setNumInputs(2);
	setNumOutputs(2);
	canProcessReplacing();

	vst_strncpy(programName, "Default", kVstMaxProgNameLen);

	for(VstInt32 i = 0; i < kParamCount; i++)
	{
		setParameter(i, getParameterDefault(i));
	}

	tp.setSampleRate(getSampleRate());
	DECLARE_VST_DEPRECATED(canMono) (true);
}

void Tilt::setParameter(VstInt32 index, float value)
{
	params[index] = value;

	switch(index)
	{
	case kParamActive: 
		tp.setBypass(value < 0.5f); break;
	case kParamFrequency: 
		tp.setFreq((float)2.0f * pow(10.0f, (0.8f + value*3.2f)));
		//(float)2.0f * pow(10.0f, (0.8f + value*3.2f));
		break;
	case kParamGain: 
		tp.setGain((value - 0.5f) * 2 * 6.0f);
		//(value - 0.5f) * 2 * 6.0f; // 6.0f db range +6/-6
		break; 
	}
}

float Tilt::getParameter(VstInt32 index)
{
	return params[index];
}

float Tilt::getParameterDefault(VstInt32 index)
{
	switch(index)
	{
	case kParamActive: return 1.f;
	case kParamFrequency: return 0.60f;
	case kParamGain: return 0.50f;
	}

	return 0.f;
}

float Tilt::getParameterFromString(VstInt32 index, char *text)
{
	double x = atof(text);
	return getParameter(index);
}

void Tilt::getParameterName(VstInt32 index, char *text)
{
	switch(index)
	{
	case kParamActive: vst_strncpy(text, "Active", kVstMaxParamStrLen); return;
	case kParamFrequency: vst_strncpy(text, "Freq.", kVstMaxParamStrLen); return;
	case kParamGain: vst_strncpy(text, "Gain", kVstMaxParamStrLen); return;
	}
	vst_strncpy(text, "", kVstMaxParamStrLen);
}

void Tilt::getParameterDisplay(VstInt32 index, char *text)
{
	switch(index)
	{
	case kParamFrequency: int2string((VstInt32) tp.getFreq(), text, kVstMaxParamStrLen); return;
	case kParamGain: float2string ((params[index] - 0.5f) * 2 * 6.0f, text, kVstMaxParamStrLen);; return;
	}
	vst_strncpy(text, (getParameter(index) >= 0.5f)?"On":"Off", kVstMaxParamStrLen);
}

void Tilt::getParameterLabel(VstInt32 index, char *label)
{
	switch(index)
	{
	case kParamGain: vst_strncpy(label, "dB", kVstMaxParamStrLen); return;
	case kParamFrequency: vst_strncpy(label, "Hz", kVstMaxParamStrLen); return;
	}
	vst_strncpy(label, "", kVstMaxParamStrLen);
}

void Tilt::getProgramName(char *name)
{
	vst_strncpy(name, programName, kVstMaxProgNameLen);
}

void Tilt::setProgramName(char *name)
{
	vst_strncpy(programName, name, kVstMaxProgNameLen);
}

void Tilt::setSampleRate(float sampleRate) {
	AudioEffectX::setSampleRate(sampleRate);
	for(int j=0; j<kParamCount; j++) {
		setParameter(j, getParameter(j));
	}
	tp.setSampleRate(sampleRate);
}

void Tilt::setProgram(VstInt32 program) {
	AudioEffectX::setProgram(program);
	for(int j=0; j<kParamCount; j++) {
		setParameter(j, getParameter(j));
	}
}

bool Tilt::getEffectName(char *name) {
	vst_strncpy(name, "Tilt", kVstMaxEffectNameLen);
	return true;
}


bool Tilt::getProductString (char *text) {
	vst_strncpy(text, "VST Plug-Ins", kVstMaxProductStrLen);
	return true;
}


bool Tilt::getVendorString (char *text) {
	vst_strncpy(text, "Rui Barreiros", kVstMaxVendorStrLen);
	return true;
}

void Tilt::processReplacing(float **inputs, float **outputs, VstInt32 sampleFrames)
{
	float *in1 = inputs[0], *in2 = inputs[1], *out1 = outputs[0], *out2 = outputs[1];
	double inL, inR, outL, outR;

	for (int i = 0; i < sampleFrames; i++) {
        inL = in1[i];
        inR = in2[i];

        tp.process(inL, inR, &outL, &outR);

        out1[i] = (float)outL;
        out2[i] = (float)outR;
    }
}

void Tilt::processDoubleReplacing(double **inputs, double **outputs, VstInt32 sampleFrames)
{
	double *in1 = inputs[0], *in2 = inputs[1], *out1 = outputs[0], *out2 = outputs[1];
	double inL, inR, outL, outR;

	for (int i = 0; i < sampleFrames; i++) {
        inL = in1[i];
        inR = in2[i];

        tp.process(inL, inR, &outL, &outR);

        out1[i] = (float)outL;
        out2[i] = (float)outR;
    }
}
